package redisinit

import (
	"context"
	"fmt"
	"srv/srv"
	"strconv"
)

func ShopAdd(ShopTitle, ShopCover, ShopStatus string, ShopPrice float64, ShopStock, UserId, ShopId int) error {
	key := fmt.Sprintf("shop:user_id_%d:shop_id_%d", UserId, ShopId)
	return Rdb.HMSet(context.Background(), key, map[string]interface{}{
		"ShopTitle":  ShopTitle,
		"ShopCover":  ShopCover,
		"ShopStatus": ShopStatus,
		"ShopPrice":  ShopPrice,
		"ShopStock":  ShopStock,
		"UserId":     UserId,
		"ShopId":     ShopId,
	}).Err()
}
func ShopList(UserId int64) (shop []*srv.ShopList, err error) {
	key := fmt.Sprintf("shop:user_id_%d:shop_id_*", UserId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, s := range keys {
		lists := Rdb.HGetAll(context.Background(), s).Val()
		ShopTitle := lists["ShopTitle"]
		ShopCover := lists["ShopCover"]
		ShopStatus := lists["ShopStatus"]
		ShopPrice, _ := strconv.ParseFloat(lists["ShopPrice"], 64)
		ShopStock, _ := strconv.Atoi(lists["ShopStock"])
		shop = append(shop, &srv.ShopList{
			ShopTitle:  ShopTitle,
			ShopStatus: ShopCover,
			ShopCover:  ShopStatus,
			ShopPrice:  float32(ShopPrice),
			ShopStock:  int64(ShopStock),
		})
	}
	return
}
